﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;
using ELGGAPI.Models;
using MySql.Data.MySqlClient;
namespace ELGGAPI.Data
{
    public class GameInstanceModels
    {
        public Guid instanceid { get; set; }
        public int gameid { get; set; }
        public string gameurl { get; set; }
        public string playerids { get; set; }
        public int? maxplayer { get; set; }
        public string startdatetime { get; set; }
    }
    public class GameDomainModel
    {
        public static string AddUpdateGameInstance(GameInstanceModels gameinstance)
        {
            string isInstanceGUID="";
            

            using (MySqlConnection connection = new MySqlConnection(APIHelper.ConnectionString))
            {
                using (MySqlCommand mySqlCommand = new MySqlCommand(APIHelper.DBName + "my_gameInstanceInsertUpdate", connection))
                {
                    mySqlCommand.Parameters.Add("pGameInstanceGUID", gameinstance.instanceid);
                    mySqlCommand.Parameters.Add("pGameID", gameinstance.gameid);
                    mySqlCommand.Parameters.Add("pUserIDs", gameinstance.playerids);
                    mySqlCommand.Parameters.Add("pGameStartDateTime", Int64.Parse(gameinstance.startdatetime));
                    

                    mySqlCommand.CommandType = CommandType.StoredProcedure;
                    mySqlCommand.Connection.Open();
                    MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                    while (mySqlDataReader.Read())
                    {
                        isInstanceGUID =mySqlDataReader["InstanceGUID"].ToString();
                    }
                    mySqlDataReader.Close();
                    connection.Close();
                }
            }
            return isInstanceGUID;
        }
        public static string DeleteGameInstance(string instanguid)
        {
            using (MySqlConnection connection = new MySqlConnection(APIHelper.ConnectionString))
            {
                using (MySqlCommand mySqlCommand = new MySqlCommand(APIHelper.DBName + "my_gameInstanceDelete", connection))
                {
                    mySqlCommand.Parameters.Add("pGameInstanceGUID", instanguid);
                    mySqlCommand.CommandType = CommandType.StoredProcedure;
                    mySqlCommand.Connection.Open();
                    mySqlCommand.ExecuteNonQuery();
                    connection.Close();
                }
            }
            return instanguid;
        }
        public static List<GameInstanceModels> GetGameInstanceList(string gameinstanceid,bool action,int playerid)
        {
            List<GameInstanceModels> list = new List<GameInstanceModels>();
            DataSet dataSet = new DataSet();
            using (MySqlConnection connection = new MySqlConnection(APIHelper.ConnectionString))
            {
                using (MySqlCommand selectCommand = new MySqlCommand(APIHelper.DBName + "my_getGameInstance", connection))
                {
                    if(!string.IsNullOrEmpty(gameinstanceid))
                        selectCommand.Parameters.AddWithValue("pGameInstanceGUID", gameinstanceid);
                    else
                        selectCommand.Parameters.AddWithValue("pGameInstanceGUID", DBNull.Value);

                    if (playerid==0)
                        selectCommand.Parameters.AddWithValue("pUserID", DBNull.Value);
                    else
                        selectCommand.Parameters.AddWithValue("pUserID", playerid);

                    selectCommand.Parameters.AddWithValue("pAction", action);

                    selectCommand.CommandType = CommandType.StoredProcedure;
                    using (MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(selectCommand))
                        ((DataAdapter)mySqlDataAdapter).Fill(dataSet);
                }
            }
            if (dataSet.Tables.Count > 0)
            {
                foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[0].Rows)
                    list.Add(new GameInstanceModels()
                    {
                        gameid = int.Parse("0"+dataRow["GameId"].ToString()),
                        instanceid = new Guid(dataRow["GameInstanceGUID"].ToString()),
                        maxplayer = int.Parse("0" + dataRow["MaxPlayer"].ToString()),
                        playerids = dataRow["userIDs"].ToString(),
                        startdatetime = dataRow["GameStartDateTime"].ToString(),
                        gameurl= dataRow["GameUrl"].ToString()
                    });
            }
            return list;
        }
    }
}